package com.nidu.demo.menu.executor.query;

import com.alibaba.cola.dto.MultiResponse;
import com.nidu.demo.menu.convertor.MenuConvertor;
import com.nidu.demo.menu.convertor.MenuCriteriaConvertor;
import com.nidu.demo.menu.dto.MenuCO;
import com.nidu.demo.menu.dto.MenuListQry;
import com.nidu.demo.menu.dto.MenuTreeCO;
import com.nidu.demo.menu.dto.MenuTreeQry;
import com.nidu.demo.menu.model.MenuCriteria;
import com.nidu.demo.menu.util.MenuUtil;
import com.nidu.demo.menu.gateway.MenuGateway;
import com.nidu.demo.menu.model.Menu;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 菜单列表查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MenuListQryExe {

    private final MenuGateway gateway;

    private final MenuConvertor convertor;

    private final MenuCriteriaConvertor criteriaConvertor;

    public MultiResponse<MenuCO> execute(MenuListQry qry) {
        MenuCriteria criteria = criteriaConvertor.toDataObject(qry);
        List<Menu> menus = gateway.listByCondition(criteria);
        List<MenuCO> menuCOS = convertor.toClientObjects(menus);
        return MultiResponse.of(menuCOS);
    }

    public MultiResponse<MenuTreeCO> execute(MenuTreeQry qry) {
        MenuCriteria criteria = criteriaConvertor.toDataObject(qry);
        List<Menu> menus = gateway.treeByCondition(criteria);
        List<MenuTreeCO> menuTreeCOS = convertor.toTreeClientObjects(menus);
        List<MenuTreeCO> treeCOS = MenuUtil.buildMenuTree(menuTreeCOS);
        return MultiResponse.of(treeCOS);
    }

}